const db = require('../db/index');
//获取座位信息
exports.getSeatInfo = function (req, res) {

    const sql = 'SELECT * FROM seats';
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err);
        }
        res.send({ status: 0, message: "获取座位信息成功！", data: results });
    });
}
//更新座位信息(座位状态)
exports.updateSeatInfo = function (req, res) {
    const sql = 'UPDATE seats SET status=? WHERE id=?';
    db.query(sql, [req.body.status, req.body.id], (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.affectedRows !== 1) {
            return res.cc("更新座位信息失败！");
        }
        res.send({ status: 0, message: "更新座位信息成功！" });
    });
}
//获取预约信息
exports.getreservation = function (req, res) {
    //获取预约信息和预约的用户信息和座位的状态
    const sql = 'SELECT reservations.*,ev_users.nickname,seats.status FROM reservations JOIN ev_users ON reservations.user_id=ev_users.id JOIN seats ON reservations.seat_id=seats.id';
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err);
        }
        res.send({ status: 0, message: "获取预约信息成功！", data: results });
    });
}

//根据id获取预约信息
exports.getreservationbyid = function (req, res) {
    const sql = 'SELECT * FROM seats WHERE id=?';
    db.query(sql, req.body.id, (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.length === 0) {
            return res.send({ status: 0, message: "获取预约成功", data: null });

        }
        res.send({ status: 0, message: "获取预约信息成功！", data: results });
    });
}

//插入预约信息
exports.insertreservation = function (req, res) {
    const sql = 'INSERT INTO reservations SET seat_id=?,reservation_date=?,reservation_time=?,user_id=?';
    db.query(sql, [req.body.seat_id, req.body.reservation_date, req.body.reservation_time, req.auth.id], (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.affectedRows !== 1) {
            return res.cc("预约失败！");
        }
        res.send({ status: 0, message: "预约成功！", data: results })
    });
}

//获取申请人信息
exports.getapplicantinfo = function (req, res) {
    //在reservation表里面根据user_id获取申请人信息，还有座位信息
    const sql = 'SELECT reservations.*,ev_users.nickname,ev_users.email,ev_users.studentId FROM reservations JOIN ev_users ON reservations.user_id=ev_users.id JOIN seats ON reservations.seat_id=seats.id WHERE reservations.reservation_status=1';
    db.query(sql, (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.length === 0) {
            return res.send({ status: 0, message: "获取申请人信息成功", data: null });
        }
        res.send({ status: 0, message: "获取申请人信息成功！", data: results });
    });
}

//拒绝申请
exports.refuse = function (req, res) {
    const sql = 'UPDATE reservations SET reservation_status=2 WHERE user_id=?';
    db.query(sql, req.body.user_id, (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.affectedRows !== 1) {
            return res.cc("拒绝失败！");
        }
        res.send({ status: 0, message: "拒绝成功！" });
    });
}

//接受申请
exports.accept = function (req, res) {
    const sql = 'UPDATE reservations SET reservation_status=3 WHERE user_id=?';
    db.query(sql, [req.body.user_id], (err, results) => {
        if (err) {
            return res.cc(err);
        }
        if (results.affectedRows !== 1) {
            return res.cc("接受失败！");
        }
        //如果时长为负数,就是占用座位，把座位状态改成2
        console.log(req.body);
        if (req.body.reservation_time === 9999) {
            const sql = 'UPDATE seats SET status=2 WHERE id=?';
            db.query(sql, [req.body.seat_id], (err, results) => {
                if (err) {
                    return res.cc(err);
                }
                if (results.affectedRows !== 1) {
                    return res.cc("接受失败！");
                }
            });
        }
        else {
            const sql = 'UPDATE seats SET status=1 WHERE id=?';
            db.query(sql, [req.body.seat_id], (err, results) => {
                if (err) {
                    return res.cc(err);
                }
                if (results.affectedRows !== 1) {
                    return res.cc("接受失败！");
                }
            });

        }
        res.send({ status: 0, message: "接受成功！" });
    });
}
